]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
environment: Add Math.clamp
authorGeorges Basile Stavracas Neto <georges.stavracas@gmail.com>
Tue, 2 Jun 2020 03:41:43 +0000 (00:41 -0300)
committerGeorges Basile Stavracas Neto <georges.stavracas@gmail.com>
Wed, 3 Jun 2020 15:55:53 +0000 (12:55 -0300)
The good old clamp function, now part of the Math family.
Clamp is happy after so many years of loneliness.

This is a strict implementation of the draft ECMAScript
proposal.

https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1295

js/ui/appDisplay.js
js/ui/environment.js
js/ui/iconGrid.js
js/ui/status/location.js
js/ui/swipeTracker.js

index 5ab871f07db0718326648dbcf4b4729856bfc84f..8931ca4a163868c78420fef7a3043097dbfcbf2b 100644 (file)
@@ -72,10 +72,6 @@ function _getFolderName(folder) {
     return name;
 }
 
-function clamp(value, min, max) {
-    return Math.max(min, Math.min(max, value));
-}
-
 function _getViewFromIcon(icon) {
     for (let parent = icon.get_parent(); parent; parent = parent.get_parent()) {
         if (parent instanceof BaseAppView)
@@ -627,7 +623,7 @@ class AppDisplay extends BaseAppView {
     }
 
     goToPage(pageNumber, animate = true) {
-        pageNumber = clamp(pageNumber, 0, this._grid.nPages() - 1);
+        pageNumber = Math.clamp(pageNumber, 0, this._grid.nPages() - 1);
 
         if (this._grid.currentPage === pageNumber &&
             this._displayingDialog &&
index a0b884eb3e49ca2ac4aa98cddac01a4579461bb2..9b4ef24b14803142e24ae893f1c3ae8bea2c6279 100644 (file)
@@ -362,6 +362,10 @@ function init() {
     const Format = imports.format;
 
     String.prototype.format = Format.format;
+
+    Math.clamp = function (x, lower, upper) {
+        return Math.min(Math.max(x, lower), upper);
+    };
 }
 
 // adjustAnimationTime:
index c0c8af37261bef606bf1728845b78a67c2e08a58..f67a70ed13df6932a945ed9e7caff8f669687fee 100644 (file)
@@ -155,10 +155,6 @@ class BaseIcon extends St.Bin {
     }
 });
 
-function clamp(value, min, max) {
-    return Math.max(Math.min(value, max), min);
-}
-
 function zoomOutActor(actor) {
     let [x, y] = actor.get_transformed_position();
     zoomOutActorAtPos(actor, x, y);
@@ -182,8 +178,8 @@ function zoomOutActorAtPos(actor, x, y) {
     let scaledHeight = height * APPICON_ANIMATION_OUT_SCALE;
     let scaledX = x - (scaledWidth - width) / 2;
     let scaledY = y - (scaledHeight - height) / 2;
-    let containedX = clamp(scaledX, monitor.x, monitor.x + monitor.width - scaledWidth);
-    let containedY = clamp(scaledY, monitor.y, monitor.y + monitor.height - scaledHeight);
+    let containedX = Math.clamp(scaledX, monitor.x, monitor.x + monitor.width - scaledWidth);
+    let containedY = Math.clamp(scaledY, monitor.y, monitor.y + monitor.height - scaledHeight);
 
     actorClone.ease({
         scale_x: APPICON_ANIMATION_OUT_SCALE,
index e5c8368e4c0d8d453faa7e0d82ba13ed2be926be..4250ed0fe9dc20b05d1bd906cbd4717b9edeca7a 100644 (file)
@@ -225,10 +225,6 @@ class Indicator extends PanelMenu.SystemIndicator {
     }
 });
 
-function clamp(value, min, max) {
-    return Math.max(min, Math.min(max, value));
-}
-
 var AppAuthorizer = class {
     constructor(desktopId, reqAccuracyLevel, permStoreProxy, maxAccuracyLevel) {
         this.desktopId = desktopId;
@@ -313,9 +309,8 @@ var AppAuthorizer = class {
 
     _completeAuth() {
         if (this._accuracyLevel != GeoclueAccuracyLevel.NONE) {
-            this._accuracyLevel = clamp(this._accuracyLevel,
-                                        0,
-                                        this._maxAccuracyLevel);
+            this._accuracyLevel = Math.clamp(this._accuracyLevel,
+                0, this._maxAccuracyLevel);
         }
         this._saveToPermissionStore();
 
index 818d101938c6fb09969b905c5f99eb2d7977fab7..a638d78019ca6e025a6c70e659cc00bc17ea1527 100644 (file)
@@ -28,10 +28,6 @@ const State = {
     SCROLLING: 1,
 };
 
-function clamp(value, min, max) {
-    return Math.max(min, Math.min(max, value));
-}
-
 const TouchpadSwipeGesture = GObject.registerClass({
     Properties: {
         'enabled': GObject.ParamSpec.boolean(
@@ -558,8 +554,8 @@ var SwipeTracker = GObject.registerClass({
 
         let firstPoint = this._snapPoints[0];
         let lastPoint = this._snapPoints[this._snapPoints.length - 1];
-        this._progress = clamp(this._progress, firstPoint, lastPoint);
-        this._progress = clamp(this._progress,
+        this._progress = Math.clamp(this._progress, firstPoint, lastPoint);
+        this._progress = Math.clamp(this._progress,
             this._initialProgress - 1, this._initialProgress + 1);
 
         this.emit('update', this._progress);
@@ -606,7 +602,7 @@ var SwipeTracker = GObject.registerClass({
 
         let duration = Math.abs((this._progress - endProgress) / velocity * DURATION_MULTIPLIER);
         if (duration > 0) {
-            duration = clamp(duration,
+            duration = Math.clamp(duration,
                 MIN_ANIMATION_DURATION, MAX_ANIMATION_DURATION);
         }